/**
 * \file screenpig/include/main.c
 * \date 01-21-2010
 * \author Davide Gessa
 */
#include "include/render.h"
#include "include/window.h"
#include "include/events.h"
#include "include/term.h"
#include "include/bar.h"
#include "main.h"
#include <arch.h>
#include <driver/video/vesa.h>
#include <driver/input/keyboard.h>
#include <driver/input/mouse.h>
#include <stdio.h>


/**
 * Main di screenpig
 */
int screen_pig_main(int ax, int ay)
{
	event_mouse_t m;
	
	res_x = ax;
	res_y = ay;
	
	keyboard_init();
	mouse_init();

	window_load_default_dec();
	
	cur_x = ax / 2;
	cur_y = ax / 2;
	
	window_list = NULL;
	char *phys = (char *) (((vesa_mode_info_t *) vesa_get_mode_info())->phys_base_ptr);

	window_t *a = window_new("term", 150, 150, 400, 250);
	char buf[] = "ciaooo!! test di buffer visualizzato\na schermo, \nbla \nbla blabla asdjaskdjkassakjdkjcvkjxkjvkvdjxkkskdjfkjdfskj";
	screen_term_t term = { a->x, a->x + a->size_x, a->y, a->y + a->size_y, 16, 0xFFFFFFF, 0x0, buf, strlen(buf) };


	window_new("pressed", 100-2, 40, 100+10, 66);
	//window_new("config", 450, 100, 300, 100);
	
			
	render_draw_rect_fill(0, 0, res_x + 200, res_y + 200, 0x555555);//0A8D5);
	
	char menuaa[70*70*4];
	load_raw_bitmap("/initr/menu.raw", menuaa, 70, 70);


	// Loop di rendering
	while(1)
	{
		// Sfondo
		//render_draw_rect_fill(0, 0, res_x, res_y, 0x00B8F5);
		
		// Visualizza tutte le finestre
		window_t *win = window_list;
		
		//bar_render();

		//render_bit_map((uint32_t *) menuaa, 4, ay - 74, 70, 70);

		while(win != NULL)
		{
			//win->x++;
			window_render(win);
		
			win = win->next;
		}
		
		// Visualizza il terminale di test
		//term_render(&term);
		
		// Test di input
		char a = events_key_get();
		char b[2];
		b[0] = a;
		b[1] = '\0';
		
		if(a != 0)
		{
			render_write_letter(16, a, 0x0, 110, 50);
			window_list->if_updated = 1;
		}
		// Disegna il cursore
		//m = events_mouse_get();
		//render_draw_cursor(100, 100); //cur_x, cur_y);
		
		render_swap_buffer(phys, render_buffer, sizeof(render_buffer));
		//arch_sleep(1);
	}
	
	
	return 0;
}
